﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookingManagementSystem.Models;


namespace BookingManagementSystem.Controllers
{
    public class BookingsController : Controller
    {
        //
        // GET: /Bookings/

        public ActionResult Index()
        {
            var bookings = DataAccessLayer.GetAllBookings();
            return View(bookings);
        }

        [HttpGet]
        public ActionResult CreateBooking()
        {
            BookingModel newBooking = new BookingModel(DataAccessLayer.GetAllCustomers(), DataAccessLayer.GetAllRooms(), DataAccessLayer.GetAllOrigins());
            return View("BookingView",newBooking);
        }

        [HttpPost]
        public ActionResult CreateBooking(BookingModel booking)
        {
            if (ModelState.IsValid)
            {
                DataAccessLayer.AddBooking(booking);
                return RedirectToAction("Index");
            }
            else
            {
                booking.AllCustomers = DataAccessLayer.GetAllCustomers();
                booking.AllOrigins = DataAccessLayer.GetAllOrigins();
                booking.AllRooms = DataAccessLayer.GetAllRooms();
                return View("BookingView", booking);
            }
        }

        public void DeleteBooking(int idBooking)
        {
            DataAccessLayer.DeleteBooking(idBooking);
        }

        [HttpGet]
        public ActionResult EditBookingById(int idBooking)
        {
            var booking = DataAccessLayer.GetBookingById(idBooking);
            booking.AllCustomers = DataAccessLayer.GetAllCustomers();
            booking.AllOrigins = DataAccessLayer.GetAllOrigins();
            booking.AllRooms = DataAccessLayer.GetAllRooms();
            return View("BookingView", booking);
        }

        [HttpPost]
        public ActionResult EditBookingById(BookingModel booking)
        {
            if (ModelState.IsValid)
            {
                DataAccessLayer.EditBooking(booking);
                return RedirectToAction("Index");
            }
            else
            {
                booking.AllCustomers = DataAccessLayer.GetAllCustomers();
                booking.AllOrigins = DataAccessLayer.GetAllOrigins();
                booking.AllRooms = DataAccessLayer.GetAllRooms();
                return View("BookingView", booking);
            }
        }
    }
}
